package com.iflytek.jnitest.jnitest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView jniMsgView;
    private Button btnWildException;
    private Button btneOutOfBound;
    private Button btneDividedZero;
    private Button btneFormatException;
    private Button btnOutOfBuffer;
    private Button btneSigAbrt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        addListener();

        initData();
    }

    private void initView() {
        jniMsgView = (TextView) findViewById(R.id.jni_msgView);

        btnWildException = (Button) findViewById(R.id.btn_wild_exception);

        btneOutOfBound = (Button) findViewById(R.id.btn_out_of_bound_exception);

        btneDividedZero = (Button) findViewById(R.id.btn_divided_0);

        btneFormatException = (Button) findViewById(R.id.btn_format_Exception);

        btnOutOfBuffer = (Button) findViewById(R.id.btn_out_of_buffer);

        btneSigAbrt = (Button) findViewById(R.id.btn_sig_abrt);
    }

    private void initData() {
        jniMsgView.setText(new TestJni().getMsgFromJni());
    }

    private void addListener() {
        btnWildException.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeWildException();
            }
        });

        btneOutOfBound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeOutOfBound();
            }
        });

        btneDividedZero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeDividedZero();
            }
        });

        btneFormatException.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeFormatException();
            }
        });

        btnOutOfBuffer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeOutOfBuffer();
            }
        });

        btneSigAbrt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeSigAbrt();
            }
        });

    }

    private void makeWildException() {
        TestJni.makeWildPointException();
    }

    private void makeOutOfBuffer() {
        TestJni.makeOutOfBuffer();
    }

    private void makeFormatException() {
        TestJni.makeFormatException();
    }

    private void makeOutOfBound() {
        TestJni.makeOutOfBoundException();
    }

    private void makeDividedZero() {
        TestJni.makeDividedZero();
    }

    private void makeSigAbrt() {
        TestJni.makeSigAbrt();
    }
}
